
// console.clear();
console.log("加载index.js文件")

// var socket = io('http://119.29.22.196:7080');
// socket.on('connect', function(){
// console.log("socketio 连接成功");
// });
// socket.on('event', function(data){});
// socket.on('disconnect', function(){
//     console.log("socketio 断开连接");
// });

// function connect () {
//     socket.on("s:send", function(type, data, fn) {
//         if(type=="room"){
//             fn({userId:"",msgId:""});
//         }else{
//             fn("ac");
//         }
//         console.log(data);
//     });
//     socket.connect();
// }

var app = new Vue({
  el: "#app",
  data: {
    projectIds: [],
    socketUrl: "119.29.22.196:7080",
    connecting: false,
    reconnectMaxCount: 3,
    socket: {},
    register: {
      "userId": "15196614938",
      "projectId": "net.arrcencloud.arrcenclouddemo",
      "devtype": "web",
      "devtoken": "testChat"
    },
    message: {
      "targetId": "d5b757a10eb46b717f8978caa58c6998",
      "projectId": "net.arrcencloud.njhrss",
      "content": "Hello World☺"
    }
  },
  computed: {
    // a computed getter
    connectState: function () {
      if (this.connecting) {
        return "连接中";
      }
      return this.socket.connected ? "已连接" : "连接";
    }
  },
  beforeMount: function () {
    // connect();
  },
  mounted: function() {
    this.projectIds = this.loadAll();
    this.connect(this.socketUrl)
  },
  methods: {
    querySearchProjectId: function (inputStr, cb) {
      var projectIds = this.projectIds;
      var results = inputStr ? projectIds.filter(this.createFilter(inputStr)) : projectIds;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter: function(queryString) {
      return (item) => {
        return (item.value.indexOf(queryString.toLowerCase()) === 0);
      };
    },
    handleSelect(item) {
      console.log(item);
    },
    loadAll() {
      return [{
          "value": "net.arrcencloud.arrcenclouddemo",
          "name": "组件测试平台"
        },
        {
          "value": "net.arrcencloud.njhrss",
          "name": "内江人社"
        }
      ];
    },
    socketConnectHandle() {
      if(this.socket.connected) {
        this.socket.disconnect()
      } else {
        this.connect(this.socketUrl);
      }
    },
    connect: function (url) {
      url = "http://" + url;
      console.log(url);
      this.socket = io(url);
      this.socket.on('connect', () => {
        this.connecting = false;
        console.log("socketio 连接成功");
      });
      this.socket.on('event', (data) => {
        console.log('event ===> ' + JSON.stringify(data));
      });
      this.socket.on('disconnect', () => {
        console.log("socketio 断开连接");
        this.connecting = false;
        // this.socket.connect();
      });
      this.socket.on('error', (errObj) => {
        console.log('error ===> ' + JSON.stringify(errObj));
      });
      this.socket.on('reconnect', (num) => {
        console.log('reconnect ===> ' + JSON.stringify(num));
      });
      this.socket.on('reconnect_attempt', (data) => {
        console.log('reconnect_attempt ===> ' + JSON.stringify(data));
        if(data > this.reconnectMaxCount) {
          this.socket.close();
          this.connecting = false;
        }
      });
      this.socket.on('reconnecting', (num) => {
        console.log('reconnecting ===> ' + JSON.stringify(num));
      });
      this.socket.on('reconnect_error', (errObj) => {
        console.log('reconnect_error ===> ' + JSON.stringify(errObj));
      });
      this.socket.on('reconnect_failed', (data) => {
        console.log('reconnect_failed ===> ' + JSON.stringify(data));
      });
      this.socket.on("s:send", (type, data, fn) => {
        if (type == "room") {
          fn({
            userId: "",
            msgId: ""
          });
        } else {
          fn("ac");
        }
        console.log(data);
      });
      this.connecting = true;
      this.socket.connect();
    },
    regist: function (register) {
      this.socket.emit("regist", {
        "project": register.projectId,
        "userId": register.userId,
        "resource": register.devtype,
        "token": register.devtoken
      }, function (data) {
        console.log(data);
      });
    },
    sendMessage: function (message) {
      this.socket.emit("c:send", {
        "targetId": message.targetId,
        'project': message.projectId,
        "type": "chat",
        "msg": message.content,
        "msgType": "text",
        "voiceLength": "0"
      }, function (data) {
        console.info(data);
      });
    },
    sendNotification: function (message) {
      this.socket.emit("c:send", {
        "targetIds": [message.targetId],
        'project': message.projectId,
        "type": "informs",
        "title": "消息通知测试",
        "msg": message.content,
        "msgType": "text",
        "pushType": "news"
      }, function (data) {
        console.info(data);
      });
    },
    sendFriendApply: function (message) {
      this.socket.emit("c:send", {
        "targetId": message.targetId,
        'project': message.projectId,
        "type": "request",
        "msg": message.content,
        "ftype": "1"
      }, function (data) {
        console.info(data);
      });
    }
  }
});